package com.example.splash

import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.launcher.ARouter
import com.example.base.RoutePath
import com.example.common.adapter.ImageBannerAdapter
import com.example.common.base.BaseActivity
import com.example.common.entity.BannerEntity
import com.example.room.DBUtil
import com.example.splash.databinding.ActivitySplashBinding
import com.tencent.mmkv.MMKV
import com.youth.banner.indicator.CircleIndicator
import com.youth.banner.listener.OnPageChangeListener
import com.youth.banner.transformer.AlphaPageTransformer

class SplashActivity : BaseActivity<ActivitySplashBinding>() {
    private lateinit var mImgAdapter: ImageBannerAdapter
    private var bannerData = mutableListOf<BannerEntity>()
    private lateinit var mCountDownTimer: CountDownTimer
    override fun initViews() {
        var isF = MMKV.defaultMMKV().decodeBool("isFirst", true)
        if(isF){
            MMKV.defaultMMKV().encode("isFirst", false)

            mDataBinding.clSplashApp.visibility = View.VISIBLE

            bannerData.add(BannerEntity(R.drawable.welcome_0, ""))
            bannerData.add(BannerEntity(R.drawable.welcome_1, ""))
            bannerData.add(BannerEntity(R.drawable.welcome_2, ""))

            mImgAdapter = ImageBannerAdapter(bannerData)
            mDataBinding.appBanner.setAdapter(mImgAdapter)

            //与当前Activity的生命周期关联
            mDataBinding.appBanner.addBannerLifecycleObserver(this)
            //设置指示器的样式
            mDataBinding.appBanner.indicator = CircleIndicator(this)
            //设置切换过度效果
            mDataBinding.appBanner.addPageTransformer(AlphaPageTransformer())

            //设置切换事件监听
            mDataBinding.appBanner.addOnPageChangeListener(object : OnPageChangeListener {
                override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

                }

                override fun onPageSelected(position: Int) {
                    if (position == bannerData.size - 1){
                        mDataBinding.tvStart.visibility = View.VISIBLE
                    }else{
                        mDataBinding.tvStart.visibility = View.INVISIBLE
                    }
                }

                override fun onPageScrollStateChanged(state: Int) {

                }
            })

            //立即进入按钮的点击事件
            mDataBinding.tvStart.setOnClickListener {
                jumpToMainPage()
            }
        }  else{
            mDataBinding.clSplashAdv.visibility = View.VISIBLE

            mCountDownTimer = object : CountDownTimer(5000, 1000) {

                override fun onTick(millisUntilFinished: Long) {
                    mDataBinding.tvSplashSkip.text = "${millisUntilFinished / 1000}s"
                }

                override fun onFinish() {
                    this@SplashActivity.jumpToMainPage()
                }
            }.start()
        }
    }
    override fun getLayoutId(): Int  = R.layout.activity_splash
    private fun jumpToMainPage(){
        ARouter.getInstance().build(RoutePath.ROUTE_LOGIN).navigation()
        //ARouter.getInstance().build(RoutePath.PATH_MAIN_PAGE).navigation(this@SplashActivity, LocalDegradeService(RoutePath.PATH_LOGIN_PAGE, "login")) //使用局部降级策略，此时全局降级策略将不会执行
        finish()
    }
    override fun onStart() {
        super.onStart()
        DBUtil.initDB(applicationContext)
    }
}